package com.ojoin.trade.t0loan_core.web.robot;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.message.BufferedHeader;
import org.apache.http.protocol.HTTP;

public class JD {
	// The configuration items
	private static String userName = "jd_4d3791da589fb";
	private static String password = "JIURUNYUAN111111";

	private static String redirectURL = "http://vcp.jd.com/index/initIndexPage";
//	private static String loginUrl = "http://passport.jd.com/uc/login";
	private static String loginUrl = "https://vcp.jd.com/sub_reports/main";

	// Don't change the following URL
	private static String renRenLoginURL = "https://passport.jd.com/uc/loginService";

	// The HttpClient is used in one session
	private HttpResponse response;
	private DefaultHttpClient httpclient = new DefaultHttpClient();

	public Map<String, String> getParams() {
		Map<String, String> map = new HashMap<String, String>();
		String str = getText(loginUrl);
		String strs1[] = str.split("name=\"uuid\" value=\"");
		String strs2[] = strs1[1].split("\"/>");
		String uuid = strs2[0];
		map.put("uuid", uuid);
		System.out.println(strs2[0]);
		String str3s[] = strs1[1]
				.split("<input type=\"hidden\" name=\"");
		
		if (str3s == null || str3s.length == 0)
			return map;
		
		for(int i=0; i<str3s.length; i++) {
			if (i == 0)
				continue;
			String strs4[] = str3s[i].split("/>");
			String[] str4s = strs4[0].split("\"");
			if (strs4[0].indexOf("id") == -1) {
				String key = str4s[0];
				String value = str4s[2];
				map.put(key, value);
			} else {
				String key = str4s[0];
				String value = str4s[4];
				map.put(key, value);
			}
		}
//		String strs4[] = str3s[1].split("/>");
//		String strs5[] = strs4[0].trim().split("\"");
//		String key = strs5[0];
//		String value = strs5[2];
//		map.put(key, value);
		return map;
	}

	private boolean login() {
		Map map = getParams();

		HttpPost httpost = new HttpPost(renRenLoginURL);
		// All the parameters post to the web site
		List<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>();
		nvps.add(new BasicNameValuePair("ReturnUrl", redirectURL));
		nvps.add(new BasicNameValuePair("loginname", userName));
		nvps.add(new BasicNameValuePair("nloginpwd", password));
		nvps.add(new BasicNameValuePair("loginpwd", password));
		Iterator it = map.keySet().iterator();
		while (it.hasNext()) {
			String key = it.next().toString();
			String value = map.get(key).toString();
			nvps.add(new BasicNameValuePair(key, value));

		}

		try {
			httpost.setEntity(new UrlEncodedFormEntity(
					(List<? extends org.apache.http.NameValuePair>) nvps,
					HTTP.UTF_8));
			response = httpclient.execute(httpost);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			httpost.abort();
		}
		return true;
	}

	private String getRedirectLocation() {
		BufferedHeader locationHeader = (BufferedHeader) response
				.getFirstHeader("Location");
		if (locationHeader == null) {
			return null;
		}
		return locationHeader.getValue();
	}

	private String getText(String redirectLocation) {
		HttpGet httpget = new HttpGet(redirectLocation);
		ResponseHandler<String> responseHandler = new BasicResponseHandler();
		String responseBody = "";
		try {
			responseBody = httpclient.execute(httpget, responseHandler);
		} catch (Exception e) {
			e.printStackTrace();
			responseBody = null;
		} finally {
			httpget.abort();
			// httpclient.getConnectionManager().shutdown();
		}
		return responseBody;
	}

	public void printText() {
		if (login()) {
			System.out.println(getText(redirectURL));
			String redirectLocation = getRedirectLocation();
			if (redirectLocation != null) {
				System.out.println(getText(redirectLocation));
			}
		}
	}

	public static void main(String[] args) {
		JD renRen = new JD();
		// renRen.getParams();
		renRen.printText();
	}
}